package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaOrgDao;
import com.qymerge.manage.base.dao.IMetaProfitReportItemDao;
import com.qymerge.manage.base.ro.MetaOrgRO;
import com.qymerge.manage.base.ro.MetaProfitReportItemRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.util.*;

/**
 *<p>Title: MetaProfitReportItemDaoImpl</p>
 *<p>Description: 报表项目配置</p>
 *
 * @author Xiao
 * @date 2025/5/2 01:23
 * @version 1.0.0
 */
@Repository
public class MetaProfitReportItemDaoImpl extends BaseDao implements IMetaProfitReportItemDao {

    private String dwd_MCRS_MDM_meta_ProfitReportItem = "SELECT Id,Name,ShowName,TemplateId,VisOrder,ParentId,Type,Level,CalMethod,Remark FROM dwd_MCRS_MDM_meta_ProfitReportItem WHERE 1=1 ";

    @Override
    public List list(MetaProfitReportItemRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_meta_ProfitReportItem;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql += " and Id=:Id";
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql += " and Name=:Name";
            paramMap.put("Name", "%" + ro.getName() + "%");
        }


        // 分页数据获取
        sql += " order by Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaProfitReportItemRO>(MetaProfitReportItemRO.class));
    }

    @Override
    public int total(MetaProfitReportItemRO ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_meta_ProfitReportItem where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql += " and Id=:Id";
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql += " and Name=:Name";
            paramMap.put("Name", "%" + ro.getName() + "%");
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public List<MetaProfitReportItemRO> findAll(MetaProfitReportItemRO ro) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_ProfitReportItem);
        Map<String, Object> paramMap = new HashMap<String, Object>();

        sql.append(" and TemplateId=:TemplateId");
        paramMap.put("TemplateId", ro.getTemplateId());

        sql.append(" order by VisOrder desc");
        return getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new BeanPropertyRowMapper<MetaProfitReportItemRO>(MetaProfitReportItemRO.class));
    }

    @Override
    public MetaProfitReportItemRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_ProfitReportItem);
        sql.append("and Id=?");
        RowMapper<MetaProfitReportItemRO> rowMapper = new BeanPropertyRowMapper<MetaProfitReportItemRO>(MetaProfitReportItemRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<MetaProfitReportItemRO> findAllByParentId(Long id) {
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        String sql = "select * from dwd_MCRS_MDM_meta_ProfitReportItem where ParentId in(:ids)";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("ids", id);
        return jdbc.query(sql, paramMap, new BeanPropertyRowMapper<MetaProfitReportItemRO>(MetaProfitReportItemRO.class));
    }

    @Override
    public int insert(MetaProfitReportItemRO ro) {
        StringBuilder sql = new StringBuilder("insert into dwd_MCRS_MDM_meta_ProfitReportItem(Name, ShowName, TemplateId, VisOrder, ParentID, Type, Level, CalMethod, Remark) values(?,?,?,?,?,?,?,?,?)");

        List params = new ArrayList();
        params.add(ro.getName());
        params.add(ro.getShowName());
        params.add(ro.getTemplateId());
        params.add(ro.getVisOrder());
        params.add(ro.getParentId());
        params.add(ro.getType());
        params.add(ro.getLevel());
        params.add(ro.getCalMethod());
        params.add(ro.getRemark());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int update(MetaProfitReportItemRO ro) {
        StringBuilder builder = new StringBuilder("update dwd_MCRS_MDM_meta_ProfitReportItem set Name=?, ShowName=?, VisOrder=?, Type=?, CalMethod=?, Remark=? where Id=?");

        List params = new LinkedList();
        params.add(ro.getName());
        params.add(ro.getShowName());
        params.add(ro.getVisOrder());
        params.add(ro.getType());
        params.add(ro.getCalMethod());
        params.add(ro.getRemark());
        params.add(ro.getId());

        return getJdbcTemplate().update(builder.toString(), params.toArray());
    }

    @Override
    public int remove(List<Long> ids) {
        // 删除
        String sql="delete from dwd_MCRS_MDM_meta_ProfitReportItem where Id=?";
        final List<Long> idss = ids;
        BatchPreparedStatementSetter setterMid = new BatchPreparedStatementSetter(){

            @Override
            public int getBatchSize(){
                return idss.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i){
                Long id = idss.get(i);
                try {
                    ps.setLong(1, id);
                } catch(Exception e){
                    logger.error("batchInsertWaitAward Exception:", e);
                }
            }
        };

        return getJdbcTemplate().batchUpdate(sql, setterMid).length;
    }

    @Override
    public int remove(Long Id) {
        // 删除
        String sql="delete from dwd_MCRS_MDM_meta_ProfitReportItem where Id=?";
        return getJdbcTemplate().update(sql, Id);
    }


}
